package com.gitee.wsl.common.chart.render.background.axis

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import com.gitee.wsl.android.ui.ext.type.drawRotateText
import com.gitee.wsl.android.ui.ext.type.textHeight
import com.gitee.wsl.android.ui.ext.type.textWidth
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.ext.number.round

import com.gitee.wsl.math.Math
import com.gitee.wsl.jvm.math.sub

/**
 * @ClassName AxisTitle
 * @Description  图例(AxisTitle)基类
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 *
 */

class AxisTitle {
    //图例文字画笔
     val leftTitlePaint: Paint by lazy {
         Paint().apply {
             textAlign = Paint.Align.CENTER
             isAntiAlias = true
             textSize = 26f
             color = Color.rgb(255, 153, 204)
        }
    }
     val lowerTitlePaint: Paint by lazy {
        Paint().apply {
            textAlign = Paint.Align.CENTER
            isAntiAlias = true
            textSize = 26f
            color = Color.rgb(58, 65, 83)
        }
    }
     val rightTitlePaint: Paint by lazy {
        Paint().apply {
            textAlign = Paint.Align.CENTER
            isAntiAlias = true
            textSize = 26f
            color = Color.rgb(51, 204, 204)
        }
    }

    //图例文字说明
   var leftTitle = ""
   var lowerTitle = ""
   var rightTitle = ""

     var axisTitleStyle = ChartEnum.AxisTitleStyle.NORMAL
     var crossPointTitle = ""

    //偏移距离
     var leftAxisTitleOffsetX = 0f
     var rightAxisTitleOffsetX = 0f
     var lowerAxisTitleOffsetY = 0f

     var bodyRectF = RectF()

    fun render(canvas: Canvas,bodyRectF1:RectF): Boolean {
        // TODO Auto-generated method stub
        //if (null == mChart) return false
        bodyRectF=bodyRectF1
        var left = 0.0f
        var top = 0.0f
        var right = 0.0f
        var bottom = 0.0f
        /*if (mAxisTitleStyle == ChartEnum.AxisTitleStyle.ENDPOINT) {
            left = mChart.getLeft()
            top = mChart.getPlotArea().getTop()
            right = mChart.getPlotArea().getRight()
            bottom = mChart.getPlotArea().getBottom()
        } else {
            left = mChart.getLeft()
            top = mChart.getTop()
            right = mChart.getRight()
            bottom = mChart.getBottom()
        }*/
        left = bodyRectF.left
        top = bodyRectF.top
        right = bodyRectF.right
        bottom = bodyRectF.bottom
        
        if (this.leftTitle.isNotEmpty()) {
            drawLeftAxisTitle(canvas, leftTitle, left, top, right, bottom)
        }
        if (this.lowerTitle.isNotEmpty()) {
            drawLowerAxisTitle(canvas, lowerTitle, left, top, right, bottom)
        }
        if (this.rightTitle.isNotEmpty()) {
            drawRightAxisTitle(canvas, rightTitle, left, top, right, bottom)
        }

        /*
		if(mAxisTitleStyle == ChartEnum.AxisTitleStyle.ENDPOINT && mCrossPointTitle.length() > 0 )
		{						
			drawCrossAxisTitle(canvas,mCrossPointTitle,mChart.getLeft(),mChart.getTop(),
					mChart.getRight(),mChart.getBottom());
		}
		*/
        return true
    }


    /**
     * 绘制左边轴标题
     * @param axisTitle    内容
     * @param left        左边X坐标
     * @param top        上方Y坐标
     * @param right    右边X坐标
     * @param bottom    下方Y坐标
     */
    fun drawLeftAxisTitle(
        canvas: Canvas, axisTitle: String, left: Float, top: Float,
        right: Float, bottom: Float
    ) {
        //是否需要绘制轴标题
        if (axisTitle.isEmpty()) return

        //计算图列宽度		 
        val axisTitleTextHeight = leftTitlePaint.textWidth( axisTitle)

        //画布与图表1/3的地方显示
        val axisTitleTextStartX =(left + leftAxisTitleOffsetX + leftTitlePaint.textSize).round

        //轴标题Y坐标
        var axisTitleTextStartY =  if (axisTitleStyle == ChartEnum.AxisTitleStyle.ENDPOINT) {
            (top + axisTitleTextHeight).round
        } else {
            (top + (bottom - top) / 2 + axisTitleTextHeight / 2).round
        }
        // float axisTitleTextStartY = Math.round(top + (bottom - top ) /2 + axisTitleTextHeight/2);

        //得到单个轴标题文字高度     		
        var axisTitleCharHeight = 0.0f
        for (i in axisTitle.indices) {
            axisTitleCharHeight = leftTitlePaint.textWidth(axisTitle.substring(i, i + 1))
            /*axisTitleCharHeight = DrawHelper.getInstance().getTextWidth(
                mPaintLeftAxisTitle, axisTitle.substring(i, i + 1)
            )*/
            //绘制文字，旋转-90得到横向效果
            canvas.drawRotateText(
                axisTitle.substring(i, i + 1),
                axisTitleTextStartX, axisTitleTextStartY, -90f,
                 leftTitlePaint
            )
            axisTitleTextStartY -= axisTitleCharHeight
        }
    }

    /**
     * 绘制底部轴标题
     * @param axisTitle    内容
     * @param left        左边X坐标
     * @param top        上方Y坐标
     * @param right        右边X坐标
     * @param bottom    下方Y坐标
     */
    fun drawLowerAxisTitle(
        canvas: Canvas, axisTitle: String,
        left: Float, top: Float, right: Float, bottom: Float
    ) {
       
        //是否需要绘制轴标题
        if (axisTitle.isEmpty()) return

        //计算轴标题文字宽度		 
        val axisTitleTextHeight = lowerTitlePaint.textHeight
        
        var axisTitleTextStartX = 0.0f
        val axisTitleTextStartY = Math.sub(bodyRectF.bottom, axisTitleTextHeight / 2)
        
        if (axisTitleStyle == ChartEnum.AxisTitleStyle.ENDPOINT) {
            axisTitleTextStartX = right

            //左下角交叉点绘制
            if (crossPointTitle.isNotEmpty()) {
                lowerTitlePaint.textAlign = Paint.Align.LEFT
                canvas.drawRotateText(crossPointTitle, left, axisTitleTextStartY, 0f, lowerTitlePaint)
            }
            lowerTitlePaint.textAlign = Paint.Align.RIGHT
        } else {
            axisTitleTextStartX = (left + (right - left) / 2).round
        }
        canvas.drawRotateText(axisTitle, axisTitleTextStartX - lowerAxisTitleOffsetY, axisTitleTextStartY, 0f, lowerTitlePaint)
    }


    /**
     * 绘制右边轴标题
     * @param axisTitle    内容
     * @param left        左边X坐标
     * @param top        上方Y坐标
     * @param right        右边X坐标
     * @param bottom    下方Y坐标
     */
    fun drawRightAxisTitle(
        canvas: Canvas, axisTitle: String,
        left: Float, top: Float, right: Float, bottom: Float
    ) {
        
        //是否需要绘制轴标题
        if (axisTitle.isEmpty()) return

        //计算图列高度，超过最大高度要用...表示,这个后面再加		 
        val axisTitleTextHeight = rightTitlePaint.textWidth(axisTitle)
        //画布与图表1/3的地方显示
        val axisTitleTextStartX = (right - rightAxisTitleOffsetX - rightTitlePaint.textSize).round
        //轴标题Y坐标
        var axisTitleTextStartY =(top + (bottom - top - axisTitleTextHeight) / 2).round
        //得到单个轴标题文字高度     		
        var axisTitleCharHeight = 0.0f
        for (i in axisTitle.indices) {
            axisTitleCharHeight = rightTitlePaint.textWidth(axisTitle.substring(i, i + 1))
            
            //绘制文字，旋转-90得到横向效果
            canvas.drawRotateText(
                axisTitle.substring(i, i + 1),
                axisTitleTextStartX, axisTitleTextStartY, 90f,  rightTitlePaint
            )
            axisTitleTextStartY += axisTitleCharHeight
        }
    }
}